<?php

defined('BASEPATH') or die('forbidden');

class Servermap_Controller Extends Controller {

    private $inc;
	private $dbtypelist;	//数量库类型对应表
	private $platformlist;	//平台列表
	private $qudaolist;	//渠道列表 
	private $areaList;	//区号列表 
	
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));

        $this->inc = $this->config['inc'];
		$this->dbtypelist = $this->inc['dbtypelist'];
		$this->platformlist = array(
				'0' => 'all', '1' => 'ios正式', '2' => 'ios越狱', '3' => 'android', '4' => 'pc'
		);
		$this->qudaolist = $this->inc['channel_list'];
        $areadata=array();
        $server_list= get_server_list(); //区号列表
        foreach($server_list as $k=>$row) {
            $areadata[$row['SvrID']] = $row['SvrID']."-".$row['SvrName'];
        }
        $this->areaList=$areadata;
        //print_r($this->areaList);
        
        //传给页面处理
        //$this->view->assign('areaList', $this->areaList);
    }

    function index() {

        $this->view->display("datastatistics/servermap/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $rolenums = array();
        //$where=$where.' and delete_flag=0 ';
        $sql = "SELECT COUNT(id) AS num FROM server_mapping WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $selectSql = "SELECT {$colsinfo} FROM server_mapping WHERE {$where} ORDER BY {$sortname} {$sortorder}  LIMIT {$offset},{$rp}";
        $this->db->query($selectSql);
        while ($n = $this->db->get_array('default', MYSQL_ASSOC)) {
            
        	$n['platform'] = $this->platformlist[$n['platform']]."-(".$n['platform'].")";
        	$n['qudao'] = $this->qudaolist[$n['qudao']]."-(".$n['qudao'].")";
        	$n['dbtype'] = $this->dbtypelist[$n['dbtype']]."-(".$n['dbtype'].")";
        	$n['starttime'] = date('Y-m-d H:i:s',$n['starttime']);
        	//echo $n['dbtype'];
        	//print_r($this->dbtypelist);
        	
            $rows[] = array('id' => $n['id'], 'cell' => array_values($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        if (is_submit()) {
            $rowdata = $_POST['row'];

            $selectSql = "select id from server_mapping where areanum='" . $rowdata['areanum'] . "' "
                    . " and platform = '".$rowdata['platform']."' "
                    . " and qudao = '" . $rowdata['qudao']."' and dbtype = '".$rowdata['dbtype']."' ";
            //echo $selectSql;   
            $cRow = $this->db->one($selectSql);
            if ($cRow['id'] > 0) {
                $message = " add new server error area:" . $rowdata['areanum'] . "  already exists !";
                //echo $message;
                echo "<script>alert('" . $message . "');</script>";
                cp_tips('error', $rowdata['dbname']);
            } else {
                //echo "success";
				
            	//时间要转成uniux时间
            	$rowdata['starttime'] = strtotime($rowdata['starttime']);
            	//区号加上区名
                if($rowdata['areanum'] == 0){
                    $rowdata['areaname']='all';
                }else{
                    $rowdata['areaname']=$this->areaList[$rowdata['areanum']];
                }
            	$rowdata['areaname']=$this->areaList[$rowdata['areanum']];
            	//print_r($rowdata);
            	
                $this->db->insert('server_mapping', $rowdata, true);
                insert_log(translate('MENU_DATASTATISTICS_SERVERMAP'),translate('ADD'), null,$rowdata);
                cp_tips('success', $rowdata['dbname']);
            }
        }
        //
        //$platformlist = $this->inc['platform_list'];
        //$qudaolist = $this->inc['channel_list'];
        //$dbtypelist = $this->inc['dbtypelist'];
		//print_r($this->inc['platform_list']);
		//print_r($this->inc['channel_list']);
        //$platformlist = $this->form->select('row[platform]', $this->inc['platform_list'],"id='platform'",false);
        //$qudaolist = $this->form->select('row[qudao]', $this->inc['channel_list']);
        //set_area_list_view("arealist");
        $this->view->assign('areaList', $this->areaList);
        $this->view->assign("platformlist",$this->platformlist);
        $this->view->assign("qudaolist",$this->qudaolist);
        $this->view->assign("dbtypelist",$this->dbtypelist);
        $this->view->display("datastatistics/servermap/add");
    }

    function edit($id) {
        if (!$id)
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        $row = $this->db->one("SELECT id FROM server_mapping WHERE id='{$id}'");
        if (!is_array($row))
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        if (is_submit()) 
        {
            $rowdata = $_POST['row'];
            //时间要转成uniux时间
            $rowdata['starttime'] = strtotime($rowdata['starttime']);
            if($rowdata['areanum'] == 0){
                $rowdata['areaname']='all';
            }else{
                $rowdata['areaname']=$this->areaList[$rowdata['areanum']];
            }
            $result = $this->db->update('server_mapping', $rowdata, array('id' => $id));
            if ($result) {
                $rowdata['id'] = $row['id'];
                insert_log(translate('MENU_DATASTATISTICS_SERVERMAP'),translate('EDIT'), $row);
                cp_tips('success', $id);
            } else {
                error(translate('C_TIPS_ERROR_TEXT'));
            }
        }
        $row = $this->db->one("select * from server_mapping where id='" . $id . "' ");
        
        //set_area_list_view("arealist");
        $this->view->assign('areaList', $this->areaList);
        $this->view->assign("platformlist",$this->platformlist);
        $this->view->assign("qudaolist",$this->qudaolist);
        $this->view->assign("dbtypelist",$this->dbtypelist);
        
        $this->view->display("datastatistics/servermap/edit", array('row' => $row));
    }

    function del($id) {
        $c = 0;
        $r = '';
		//echo " id = $id ";
        //if ($this->db->update('server_mapping', array('delete_flag'=>1),array('id' => $id))) 
        if ($this->db->delete('server_mapping', array('id' => $id))) {
            insert_log(translate('MENU_DATASTATISTICS_SERVERMAP'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = translate('C_TIPS_SUCCESS_TEXT');
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            if ($type == 'delete') {
                $this->db->query("DELETE FROM server_mapping WHERE id IN ({$ids})");
                $c = 1;
                insert_log(translate('MENU_DATASTATISTICS_SERVERMAP'),translate('MULTI_DEL'), array('ids'=>implode(",", $allids)));
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

}
